#pragma once 
#include <iostream>
#include <unistd.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cstring>
#include <functional>
#include "InetAddr.hpp"
#include "Log.hpp"
#include "ThreadPool.hpp"
using namespace MyLogModule;
using namespace std;
using namespace MyThreadPoolMudule;
#define CONV(TYPE) (sockaddr*)(TYPE)
//最大的连接数
#define BACKLOG 8 
using func_t = std::function<string(string&s)>;
using task_t = std::function<void()>;

class udp_server
{
public:
    udp_server(uint16_t port = 8080)
        :_port(port)
    {

    }
    void Init(){
        _fd = socket(AF_INET,SOCK_STREAM,0);
        if (_fd < 0){
            cerr << "socket create error" << endl;
            exit(-1);
        }
        //bind操作
        sockaddr_in local;
        local.sin_family = AF_INET;
        local.sin_port = ::htons(_port); //把本地字节序列转化为网络字节序列
        local.sin_addr.s_addr = INADDR_ANY; //指的是这个服务器接受来则任何ip的ip访问
        socklen_t len = sizeof(local);

        if(::bind(_fd,CONV(&local),len) < 0){
            cerr << "bind error" << endl;
            exit(-1);
        }
        if (listen(_fd,BACKLOG) < 0){
            cerr << "listen error" << endl;
            exit(1);
        }
        cout << "server 初始化成功" << endl;
    }
    void Insert(func_t handler){
        _handler = handler;
    }
    void HandlerRequest(int socketfd,InetAddr peer){
        while(1){
                cout << "开始IO" << endl;
                char buffer[1024];
                int n = ::recv(socketfd,buffer,sizeof(buffer)-1,0);
                if (n > 0){
                    buffer[n] = 0; //草了这里一定要复制为0 ,不然就会有多余的内容!!!!!!
                    LOG(LogLevel::DEBUG) << "server recive: " << n << "byte";
                    for (int i = 0; i < n; i++){
                        printf("%02x ",(unsigned char)buffer[i]);
                    }
                    printf("\n");
                    string message = buffer;
                    LOG(LogLevel::DEBUG) << peer.NetAddr() << " say#" << message;
                    string return_str = _handler(message);
                    if (return_str.empty()) continue; //说明没有一条完整的报文传过来, 接着读取
                    send(socketfd,return_str.c_str(),return_str.size(),0);
                }
                else if(n == 0){
                    cout << "client quit" << endl;
                    break;
                }
                else {
                    return;
                }
            }
    }
    void Start(){
        while(true){
            sockaddr_in peer;
            memset(&peer,0,sizeof(peer));
            socklen_t Len = sizeof(peer);  
            int socketfd = accept(_fd,(sockaddr*)(&peer),&Len);       
            if (socketfd < 0){
                cout << "accept error" << endl;
                exit(-1);
            }   
            InetAddr Peer(peer);
            ThreadPool<task_t>::GetInstance()->Enque([this,socketfd,Peer](){
                this->HandlerRequest(socketfd,Peer);
            });
        }
    }
    ~udp_server(){
        ::close(_fd);
    }
private:
    int _fd;
    uint16_t _port;
    func_t _handler;
};

void Useage(const string& message){
    cout << "Useage: " << message << "+  serverport" << endl;
}
